package top.didi.study.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import top.didi.study.vo.PageVO;


import java.io.Serial;
import java.io.Serializable;

//lombok的@Datea注解会自动生成getter/setter/toString/equals/hashCode方法
@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private boolean success;
    private T data;

    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setCode(200);
        r.setMsg("");
        r.setData(data);
        return r;
    }

    public static <T> R<T> ok(String msg,T data) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail(String message, T data) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(500);
        r.setMsg(message);
        r.setData(data);
        return r;
    }

    public static <T> R<PageVO<T>> ok(IPage<T> page) {
        PageBean pageBean = PageBean.instance(page.getCurrent(),page.getSize());
        pageBean.setTotal(page.getTotal());
        pageBean.setPageCount(page.getPages());
        PageVO<T> vo = new PageVO<>();
        vo.setList(page.getRecords());
        vo.setPageBean(pageBean);
        return R.ok(vo);
    }

}
